package zuoye;

public class Zuoye02 {
	public static void main(String[] args) {
		Function fc = new Function();
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				for(int i=0;i<3;i++){
					fc.subFunction();
				}
			}
		}).start();
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				for(int i=0;i<3;i++){
					fc.mainFunction();
				}
				
			}
		}).start();
	}

}

class Function{
	private boolean flag = false;
	
	public synchronized void mainFunction(){
		while(!flag){
			try {
			    this.wait();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
			for(int i=0;i<5;i++){
				System.out.println("mainFunction -->"+i);
			}
			this.notify();
			flag = false;
		}
	
	public synchronized void subFunction(){
		while(flag){
			try {
				this.wait();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
			for(int i=0;i<3;i++){
				System.out.println("subFunction -->"+i);
			}
		    this.notify();
		    flag = true;
	}
}
